package net.qiqbframework.domains.record.infra.persistence;

import net.qiqbframework.eventsourcing.dao.DomainEventProcessDao;
import net.qiqbframework.eventsourcing.dao.DomainEventRecordPo;
import net.qiqbframework.domains.record.domain.DomainEventRecord;
import net.qiqbframework.persisthanding.PersistHandler;
import net.qiqbframework.persisthanding.PersistType;

import java.util.List;

public class DomainEventProcessPersistence {

    private final DomainEventProcessDao domainEventProcessDao;


    public DomainEventProcessPersistence(DomainEventProcessDao domainEventProcessDao) {
        this.domainEventProcessDao = domainEventProcessDao;
    }


    @PersistHandler(type = PersistType.ADD)
    public void add(DomainEventRecord domainEventRecord) {
        domainEventProcessDao.batchInsertPos(List.of(formatPO(domainEventRecord)));
    }

    @PersistHandler(type = PersistType.MODIFY)
    public void modify(DomainEventRecord newDomainEventProcess, DomainEventRecord oldDomainEventProcess) {
        domainEventProcessDao.dynamicUpdateById(formatPO(newDomainEventProcess), formatPO(oldDomainEventProcess));
    }

    private DomainEventRecordPo formatPO(DomainEventRecord domainEventProcess) {
        // final DomainEventInfo domainEventInfo = domainEventProcess.getDomainEventInfo();
        DomainEventRecordPo processPo = new DomainEventRecordPo();
        processPo.setId(domainEventProcess.getEventId());
        processPo.setInstanceName(domainEventProcess.getInstanceName());

        return processPo;
    }

}
